<template>
	<view class="charts-box">
		<qiun-data-charts type="pie" :opts="opts" :chartData="chartData" />
	</view>
</template>

<script>
	export default {
		// 定义接收外部数据的props
		props: {
			// 接收饼图的数据
			columnData: {
				type: Array,
				default: () => []
			}
		},
		data() {
			return {
				chartData: {},
				opts: {
					color: ["#1890FF", "#91CB74", "#FAC858", "#EE6666", "#73C0DE", "#3CA272", "#FC8452", "#9A60B4",
						"#ea7ccc"
					],
					padding: [5, 5, 5, 5],
					enableScroll: false,
					extra: {
						pie: {
							activeOpacity: 0.5,
							activeRadius: 10,
							offsetAngle: 0,
							labelWidth: 15,
							border: true,
							borderWidth: 3,
							borderColor: "#FFFFFF"
						}
					}
				}
			};
		},
		watch: {
			// 监听columnData变化，更新chartData
			columnData(newVal) {
				this.updateChartData(newVal);
			}
		},
		mounted() {
			// 组件挂载时，若有初始数据则更新图表
			if (this.columnData.length > 0) {
				this.updateChartData(this.columnData);
			}
		},
		methods: {
			updateChartData(data) {
				let seriesData = data.map(item => ({
					name: item.text,
					value: item.value
				}));
				let res = {
					series: [{
						data: seriesData
					}]
				};
				this.chartData = JSON.parse(JSON.stringify(res));
			}
		}
	};
</script>

<style scoped>
	.charts-box {
		width: 90%;
		height: 260px;
	}
</style>